{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline \n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ejercicio: La ruleta\n",
    "Vamos a jugar a la ruleta, apostando siempre a color. Es ruleta europea: 18 valores rojos, 18 negros y el `00` (con el que todos los que apuestan pierden). Partimos de $v_0$ euros, y nuestro objetivo es apostar continuamente 1 euro a color, hasta que o bien logramos la cantidad $k$ objetivo, o bien nos quedamos sin dinero. Programar la funcion `ruletaObjetivo` que nos devuelve la lista con nuestro *bankroll* en cada momento."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import random\n",
    "def ruletaObjetivo(v0, k):\n",
    "    probGanar = 18 / 37\n",
    "    recorrido = [v0]\n",
    "    while v0 not in (0, k):\n",
    "        if random.random() < probGanar:\n",
    "            v0 += 1\n",
    "        else:\n",
    "            v0 -= 1\n",
    "        recorrido.append(v0)\n",
    "    return recorrido\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Suponemos que nuestro objetivo es llegar a 20, partiendo de 6."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "inicial = 6\n",
    "objetivo = 20\n",
    "disponible = ruletaObjetivo(inicial, objetivo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(1, 1)\n",
    "plt.plot(disponible)\n",
    "ax.set_xlabel('Num Jugadas')\n",
    "ax.set_ylabel('Disponible')\n",
    "ax.set_title('Jugando a la ruleta')\n",
    "ax.plot([0, len(disponible)-1], [objetivo, objetivo], color='red', lw=1, linestyle='dashdot', alpha=0.6)\n",
    "ax.plot([0, len(disponible)-1], [0, 0], color='black', lw=2);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
